<?php
class column extends table
{
	protected $_name	= 'my_class';
	protected $fields	= array('classid', 'classid',	'wid',	'bclassid', 'sonclassid', 'classname', 'bname', 'classtype', 'islast', 'classpath', 'modid', 'domain', 'classimg', 'classtitle', 'classkeyword', 'intro', 'listtemp', 'contenttemp', 'defaultname',	'classextension', 'contentextension', 'content','display');
	protected $_primary = 'classid';
	
	public function fetchAll($where=null,$order=null,$count=null,$offset=null,$listcls=true)
	{
		$data	= parent::fetchAll($where,$order,$count,$offset);

		foreach ($data as $key => $val)
		{
			if (isset($val['sonclassid']))
			{
				$data[$key]['sonclassid']	= explode("|",$val['sonclassid']);
				$data[$key]['sonclassid']	= array_filter($data[$key]['sonclassid']);
			}
			if (isset($val['domain']) || isset($val['classpath']))
			{
				$data[$key]['url']			= empty($val['domain']) ? $val['classpath'] : $val['domain'];
				//$data[$key]['url']		   .= '/';
			}
		}
		if ($listcls)
		{
			$data	= $this->listcls($data, 0);
		}
		return $data;
	}
	
	public function fetchRow($where=null,$order=null,$count=null,$offset=null)
	{
		$data	= parent::fetchRow($where,$order,$count,$offset);

			if (isset($data['sonclassid']))
			{
				$data['sonclassid']	= explode("|",$data['sonclassid']);
				$data['sonclassid']	= array_filter($data['sonclassid']);
			}
			if (isset($data['domain']) || isset($data['classpath']))
			{
				$data['url']			= empty($data['domain']) ? $data['classpath'] : $data['domain'];
				//$data['url']		   .= '/';
			}
		return $data;
	}

	public function insert($arg)
	{
		$insert = array(
	        'bclassid'          => $arg['bclassid'],
			'wid'				=> $arg['wid'],
	        'sonclassid'        => '',
	        'classname'         => $arg['classname'],
	        'bname'             => $arg['bname'],
	        'classtype'         => $arg['classtype'],
	        'islast'            => $arg['islast'],
	        'classpath'         => $arg['classpath'],
	        'modid'             => $arg['modid'],
	        'domain'            => $arg['domain'],
	        'classimg'          => $arg['classimg'],
	        'classtitle'        => $arg['classtitle'],
	        'classkeyword'      => $arg['classkeyword'],
	        'intro'             => $arg['intro'],
	        'listtemp'          => $arg['listtemp'],
	        'contenttemp'       => $arg['contenttemp'],
			'defaultname'       => $arg['defaultname'],
	        'classextension'    => $arg['classextension'],
	        'contentextension'  => stripslashes($arg['contentextension']),
	        'content'           => stripslashes($arg['content']),
			'display'			=> $arg['display'],
		);
		
		$classid = parent::insert($insert);
		if ($arg['bclassid'] !== '0')
		{
			$db	= $this->getAdapter();
			$sql = "update ".$this->_name." set sonclassid=concat(sonclassid,'|$classid') where classid={$arg['bclassid']}";
			$db->query($sql);
		}
		return $classid;
	}

	public static function listcls($data,$pid,$level = 0)
    {
        static $val = array();

        if(is_array($data))
        {
            foreach ($data as $d)
            {
                if($d['bclassid'] == $pid)
                {
                    if ($d['classid'] != '')
                    {
                        $d['level'] = $level;
                        $val[$d['classid']] = $d;
                    }
                    self::listcls($data,$d['classid'],$level+1);
                }
            }
            return $val;
        }
        return false;
    }
    
    public function fetchModids($wid)
    {
    	$db		= $this->getAdapter();
    	$select	= $db->select()->from($this->_name,array('modid'))
    		->where("wid=$wid")
    		->group('modid');
    	return $db->fetchCol($select);
    }
}
